<div class="accordion-group">
	<div class="accordion-heading">
		<a class="accordion-toggle" data-toggle="collapse" data-parent="#serveradmin" href="#reboot"> Reboot Server </a>
	</div>
	<div id="reboot" class="accordion-body collapse">
		<div class="accordion-inner">
			<form>
				<?php echo CHtml::dropdownList('id_account', '', array($whmaccount->id_account => $whmaccount->domain), array('empty' => 'Reboot Server (1 Account)')); ?><br />
				<label class="checkbox">
					<?php echo CHtml::radioButton('rebootForce', true, array('value' => 0)); ?>
					Graceful Reboot
				</label>
				<label class="checkbox">
					<?php echo CHtml::radioButton('rebootForce', false, array('value' => 1)); ?>
					Forceful Reboot
				</label> <br />
				<button class="btn btn-small btn-primary" id="reboot_btn" type="button" data-loading-text="Reboot...">Reboot Server</button>
			</form>
		</div>
	</div>
</div>

<script>
	$(document).ready(function(){
		var rebootRequest = function(){
			this.requestUrl = '';
			this.forceful = 0;
			this.id_accounts = null;

			this.beforeRequest = function(id_account){};
			this.requestCallback = function(id_account, serverReturn, doTimes){};
			this.afterDone = function(){};

			this.doTimes = 0;
			this.do = function(){
				this.doTimes++;
				var id_account = this.id_accounts.pop();
				if(!id_account){
					this.afterDone();
					return false;
				}
				this.beforeRequest(id_account);
				var thisObj = this;
				$.ajax({
					type: 'post',
					url: thisObj.requestUrl,
					data: {
						id_account: id_account,
						forceful: thisObj.forceful
					},
					success: function(ret){
						thisObj.requestCallback(id_account, ret, thisObj.doTimes);
						thisObj.do();
						return true;
					},
					dataType: 'json'
				});
			};
		};

		$('#reboot_btn').click(function(){
			var id_accounts = [];
			if($('#id_account').val() == ''){
				$('#id_account option[value!=""]').each(function(k, v){
					id_accounts.push($(v).val());
				});
			}else{
				id_accounts.push($('#id_account').val());
			}

			$('#reboot_info').blink({
				'beforeCall': function(){
					$('#reboot_info .content').append(
						'<p><b>Status Server ('+id_accounts.length+' Account)</b></p>'
					);
				},
				'callBack': function(){
					var rebootRequester = new rebootRequest();
					var length = id_accounts.length;
					rebootRequester.id_accounts = id_accounts;
					rebootRequester.forceful = $('input[name="rebootForce"]:checked').val();
					rebootRequester.requestUrl = <?php echo json_encode(url('whm/account/reboot')); ?>;
					rebootRequester.beforeRequest = function(id_account){
						$('#reboot_btn').button('loading');
						$('#reboot_info .content').append(
							'<p>'+$('#id_account option[value="'+id_account+'"]').text()+': Waiting...</p>'
						);
					};
					rebootRequester.afterDone = function(){
						$('#reboot_btn').button('reset');
					}
					rebootRequester.requestCallback = function(id_account, serverReturn, doTimes){
						if(serverReturn.result){
							$('#reboot_info .content').append(
								'<p>&nbsp;&nbsp;&nbsp;&nbsp; '+$('#id_account option[value="'+id_account+'"]').text()+' - '+serverReturn.result+' ('+doTimes+' of '+length+' Accounts)</p>'
							);							
						}else{
							$.each(serverReturn.errors, function(attrName, error){
								$(error).each(function(k, e){
									$('#reboot_info .content').append(
										'<p>&nbsp;&nbsp;&nbsp;&nbsp; '+$('#id_account option[value="'+id_account+'"]').text()+' - '+e+' ('+doTimes+' of '+length+' Accounts)</p>'
									);
								});
							});
						}
					};
					rebootRequester.do();
				},
				'showTime': 0
			});
			
		});
	});
</script>